package thread.mianshi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 *
 * 一共10000条数据  每组10条数据  每个线程处理五组， 在处理过程中，如果失败要有重试机制，
 *  重试完了还报错，需要统计成功多少条，失败多少条
 * @author wangzepeng
 * @data  2018-03-08
 */
public class DecomposeTest {
    static List<Integer> dataList = new ArrayList();
    //10000条数据
    static int dataSum = 10000;
    //每组10条数据
    static  int dataGroup = 10;
    //每个线程处理五组数据
    static int subDataGroup = 5;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //初始化数据
        for(int i = 0;i<dataSum;i++){
            dataList.add(i);
        }

        dataTask(dataList);

    }


    public static  void dataTask(List<Integer> args) throws ExecutionException, InterruptedException {
        //每组10条数据
        int size = args.size() / dataGroup;
        List<List<Integer>> subDateList = new ArrayList<>();
        for(int i = 0;i<size;i++){
            subDateList.add(dataList.subList(i*dataGroup,(i+1)*dataGroup));
        }
        //每个线程处理五组数据  50条数据   一共要开启200个线程
        int length = size/subDataGroup;
        //每5组处理
        ExecutorService executorService = Executors.newFixedThreadPool(length);
        Map<String,Integer>  result = null;
        for(int i = 0;i<length;i++){
            CallableImpl runnable = new CallableImpl();
            runnable.setList(subDateList.subList(i*subDataGroup,(i+1)*subDataGroup));
            Future<Map<String,Integer>> integerFuture = executorService.submit(runnable);
             result = integerFuture.get();
        }
        System.out.printf("成功%s条\n",result.get("success"));
        System.out.printf("失败%s条\n",result.get("fail"));


    }
}
